package org.hive.testserver.domain.model;

import java.util.SortedMap;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Summary snapshot of a TestRun for consumption by the test server outside of the logic module.
 */
public class TestRunSummary implements Comparable<TestRunSummary>{
	
	private String projectName;
	private String testRunName;
	private String testRunId;
	private SortedMap<String, String> runContext;
	private TestState currentStatus;
	private TestState result;
	private long started;
	private long duration;
	private boolean isFinished;
	private long tests = 0;
	private long passed = 0;
	private long broken = 0;
	//FIXME should this be in a ProjectSummary object?
	private ProjectPropertiesAccessor projectProperties;
	
	public TestRunSummary(TestRun testRun) {
		super();
		this.projectName = testRun.getProject().getName();
		this.testRunName = testRun.getName();
		this.testRunId = testRun.getId();
		this.runContext = testRun.getContext();
		this.currentStatus = testRun.currentStatus();
		this.result = testRun.result();
		this.started = testRun.getStarted();
		this.duration = testRun.getDuration();
		this.isFinished = testRun.isFinished();
		this.projectProperties = new ProjectPropertiesAccessor(testRun.getProject().getConfiguration().propertiesCopy());
		initializeTestsTotals(testRun);
	}
	
	private void initializeTestsTotals(TestRun testRun) {
		for (Test test : testRun.getTests()) {
			tests += 1;
			if (test.isFinished()) {
				if (test.currentState() == TestState.PASSED) {
					passed += 1;
				} else if (test.currentState() == TestState.ABORTED || test.currentState() == TestState.FAILED){
					broken += 1;
				}
			}
		}
	}
	
	public long totalTests() {
		return tests;
	}
	
	public long passedTests() {
		return passed;
	}
	
	public long brokenTests() {
		return broken;
	}

	public String getProjectName() {
		return projectName;
	}

	public String getTestRunName() {
		return testRunName;
	}

	public String getTestRunId() {
		return testRunId;
	}

	public SortedMap<String, String> getRunContext() {
		return runContext;
	}

	public TestState getCurrentStatus() {
		return currentStatus;
	}

	public TestState getResult() {
		return result;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	public boolean isFinished() {
		return isFinished;
	}

	public long getStarted() {
		return started;
	}
	
	public long getDuration() {
		return duration;
	}

	public int compareTo(TestRunSummary o) {
		return new CompareToBuilder().append(projectName, o.projectName).append(started, o.started).toComparison();
	}

	public ProjectPropertiesAccessor getProjectProperties() {
		return projectProperties;
	}
	
	public void setFinished(boolean finished) {
		this.isFinished = finished;
	}
	
	public void setTestRunState(TestState testState) {
		result = testState;
	}
	
}
